#!/QOpenSys/pkgs/bin/bash
CIJOBNAME="JENKINS_CI"
SBMJOB_OPTS="JOB($CIJOBNAME) USER(IOJS)"

export PATH=/QOpenSys/pkgs/lib/ccache:/QOpenSys/pkgs/bin:/QOpenSys/usr/bin:/usr/ccs/bin:/QOpenSys/usr/bin/X11:/usr/sbin:.:/usr/bin
export PASE_PATH=$PATH
export HOME={{ home }}/{{ server_user }}
export NODE_COMMON_PIPE="$HOME/test.pipe"
export NODE_TEST_DIR="$HOME/tmp"
export JOBS=4
export OSTYPE=ibmi
export QIBM_USE_DESCRIPTOR_STDIO=Y

unset LC_ALL

if [[ $(/QOpenSys/usr/bin/system "WRKACTJOB  JOB($CIJOBNAME)" | grep -i $CIJOBNAME | wc -l) != "1" ]]
then
  echo "Job already running"
  exit 1
fi

export START_CMD="{{ java_path[os] }} -Xmx{{ server_ram|default('128m') }} -Dos400.stdio.convert=N -Djava.net.preferIPv4Stack=true -jar {{ home }}/{{ server_user }}/slave.jar -secret {{ secret }} -jnlpUrl {{ jenkins_url }}/computer/{{ inventory_hostname }}/slave-agent.jnlp >{{ home }}/{{ server_user }}/jenkins.log 2>&1 "

echo $START_CMD

exec /QOpenSys/usr/bin/system -v -kpiveO "SBMJOB CMD(QSH CMD('echo starting Jenkins slave && cd $HOME && env && touch $HOME/jenkins.log && setccsid 1208 $HOME/jenkins.log && exec $START_CMD')) CPYENVVAR(*YES) PRTDEV(*USRPRF) ALWMLTTHD(*YES) $SBMJOB_OPTS"
